<template>
  <div>
    <div>
      <el-button type="primary"
                 @click="dialogAdd = true">添加</el-button>
    </div>
    <el-dialog :title="name"
               :visible.sync="dialogAdd"
               :width="width">
      <el-form ref="form"
               label-width="80px">
        <el-form-item label="名称">
          <el-input v-model="book.name"
                    class="ip"></el-input>
        </el-form-item>
        <el-form-item label="作者">
          <el-input v-model="book.author"
                    class="ip"></el-input>
        </el-form-item>
        <el-form-item label="价格">
          <el-input v-model="book.price"
                    class="ip"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary"
                     @click="onSubmit">立即创建</el-button>
          <el-button @click="dialogAdd = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <el-dialog title="修改书籍"
               :visible.sync="dialogUpdate"
               :width="width">
      <el-form ref="form"
               label-width="80px">
        <el-form-item label="名称">
          <el-input v-model="updateBook.name"
                    class="ip"></el-input>
        </el-form-item>
        <el-form-item label="作者">
          <el-input v-model="updateBook.author"
                    class="ip"></el-input>
        </el-form-item>
        <el-form-item label="价格">
          <el-input v-model="updateBook.price"
                    class="ip"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary"
                     @click="updateSubmit">修改</el-button>
          <el-button @click="dialogUpdate = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <el-table :data="bookList"
              style="width: 100%">
      <el-table-column prop="id"
                       label="序号"
                       width="50">
      </el-table-column>
      <el-table-column prop="name"
                       label="书名"
                       width="100">
      </el-table-column>
      <el-table-column prop="author"
                       label="作者"
                       width="120">
      </el-table-column>
      <el-table-column prop="price"
                       label="价格"
                       width="80">
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="primary"
                     size="mini"
                     @click="update(scope.row)">编辑</el-button>
          <el-button type="danger"
                     size="mini"
                     @click="del(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
export default {
  created () {
    this.getBooks();
  },

  data () {
    return {
      width: '30%',
      name: '添加书籍',
      dialogAdd: false,
      dialogUpdate: false,
      bookList: [],
      book: {},
      updateBook: {}
    }
  },
  methods: {
    getBooks () {
      this.axios.post('http://localhost:8082/query/book', {}).then(res => {
        this.bookList = res.data
      })
    },
    onSubmit () {
      this.axios.post('http://localhost:8082/book/add', this.book).then(res => {
        if (res.data.affectedRows = 1) {
          this.dialogAdd = false;
          this.getBooks()
        } else {
        }
      })
    },
    async update (row) {
      this.dialogUpdate = true;
      const book = await this.getById(row.id);
      this.updateBook = book
    },
    getById (id) {
      return new Promise((resolve) => {
        this.axios.post('http://localhost:8082/book/getById', { id: id }).then(res => {
          resolve(res.data[0]);
        })
      });
    },
    updateSubmit () {
      this.axios.post('http://localhost:8082/book/update', this.updateBook).then(res => {
        if (res.data.affectedRows = 1) {
          this.dialogUpdate = false;
          this.getBooks();
        }
      })
    },
    del (id) {
      this.axios.get('http://localhost:8082/book/del?id=' + id).then(res => {
        if (res.data.affectedRows = 1) {
          this.getBooks()
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.ip {
  width: 65%;
}
</style>